import requests
import json
from urllib import request
import base64


def get_access_token():
    appip = 'wxeeed10601755ca5a'
    scret = '057ed79bc58797e7d78a12deae4bc573'
    url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={0}&secret={1}'.format(appip, scret)
    res = requests.get(url)
    return res.json()['access_token']


def get_qrcode(scene):
    access_token = get_access_token()
    url = 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=' + access_token
    data = {
        'scene': scene,
        'page': 'pages/index/index'
    }
    data = json.dumps(data).encode(encoding='utf-8')
    header_dict = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Trident/7.0; rv:11.0) like Gecko',
                   "Content-Type": "application/json"}
    req = request.Request(url=url, data=data, headers=header_dict)
    res = request.urlopen(req)
    res = res.read()
    b64str = base64.b64encode(res)
    return b64str
